package com.lagou;

import com.lagou.service.ConsumerComponent;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class AnnotationConsumerMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        //多线程调用三个方法
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(200, 300, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

        for (int i=0; i < 6000;i++){
            Thread.sleep(20);
            threadPool.execute(()->{
                try {
                    String methodA = service.methodA("methodA");
                    String methodB = service.methodB("methodB");
                    String methodC = service.methodC("methodC");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }


        // 判断线程是否执行完毕
        threadPool.shutdown();
        try{
            while (true) {
                if (threadPool.isTerminated()) {
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>> 多线程测试性能中 >>>>>>>>>>>>>>>>>>>>>>>");
                    break;
                }
                Thread.sleep(200);
            }
        }catch (Exception e){
            System.out.println("多线程测试性能出错");
            e.printStackTrace();
        }


//        while (true) {
//            System.in.read();
//            try {
//                String hello = service.sayHello("world", 0);
//                System.out.println("result :" + hello);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }

    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou.service")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
